package com.zjj.edu.web.controller.system;

import com.zjj.edu.common.annotation.Log;
import com.zjj.edu.common.core.controller.BaseController;
import com.zjj.edu.common.core.domain.AjaxResult;
import com.zjj.edu.common.core.page.TableDataInfo;
import com.zjj.edu.common.enums.BusinessType;
import com.zjj.edu.common.utils.poi.ExcelUtil;
import com.zjj.edu.framework.util.ShiroUtils;
import com.zjj.edu.system.domain.SysTeachGoods;
import com.zjj.edu.system.domain.SysTeachGoodsRecord;
import com.zjj.edu.system.service.ISysTeachGoodsRecordService;
import com.zjj.edu.system.service.ISysTeachGoodsService;
import com.zjj.edu.system.service.ISysTenantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 教学物品异动Controller
 *
 * @author zjj
 * @date 2020-06-30
 */
@Controller
@RequestMapping("/teach/goodsRecord")
public class SysTeachGoodsRecordController extends BaseController
{
    private String prefix = "teach/goodsRecord";

    @Autowired
    private ISysTeachGoodsRecordService sysTeachGoodsRecordService;

    @Autowired
    private ISysTeachGoodsService sysTeachGoodsService;

    @Autowired
    private ISysTenantService tenantService;

    /**
     * 查询教学物品异动列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysTeachGoodsRecord sysTeachGoodsRecord)
    {
        startPage();
        sysTeachGoodsRecord.setTenantId(ShiroUtils.getTenantId());
        sysTeachGoodsRecord.setTeachGoodsId(sysTeachGoodsRecord.getTeachGoodsId());
        List<SysTeachGoodsRecord> list = sysTeachGoodsRecordService.selectSysTeachGoodsRecordList(sysTeachGoodsRecord);
        return getDataTable(list);
    }

    /**
     * 导出教学物品异动列表
     */
    @Log(title = "教学物品异动", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysTeachGoodsRecord sysTeachGoodsRecord)
    {
        sysTeachGoodsRecord.setTenantId(ShiroUtils.getTenantId());
        List<SysTeachGoodsRecord> list = sysTeachGoodsRecordService.selectSysTeachGoodsRecordList(sysTeachGoodsRecord);
        ExcelUtil<SysTeachGoodsRecord> util = new ExcelUtil<SysTeachGoodsRecord>(SysTeachGoodsRecord.class);
        return util.exportExcel(list, "goodsRecord");
    }

    /**
     * 新增保存教学物品异动
     */
    @Log(title = "教学物品异动", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysTeachGoodsRecord sysTeachGoodsRecord)
    {
        long goodsId = sysTeachGoodsRecord.getTeachGoodsId();
        SysTeachGoods goods = sysTeachGoodsService.selectSysTeachGoodsById(goodsId);
        long oldNum = goods.getNums();
        goods.setId(goodsId);
        goods.setNums(sysTeachGoodsRecord.getNewNum());
        sysTeachGoodsService.updateSysTeachGoods(goods);

        sysTeachGoodsRecord.setTenantId(ShiroUtils.getTenantId());
        sysTeachGoodsRecord.setUserId(ShiroUtils.getUserId());
        sysTeachGoodsRecord.setRecordType("数量登记");
        sysTeachGoodsRecord.setOldNum(oldNum);
        sysTeachGoodsRecord.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(sysTeachGoodsRecordService.insertSysTeachGoodsRecord(sysTeachGoodsRecord));
    }

}
